﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;

namespace TicTacToe
{
    static class Extensions
    {
        static readonly Random rng = new Random();

        public static SolidColorBrush CreateBrush(this Color color)
        {
            return new SolidColorBrush(color);
        }

        public static IntPtr GetHandle(this Visual visual)
        {
            return ((HwndSource) PresentationSource.FromVisual(visual)).Handle;
        }

        public static double Round(this double d)
        {
            return Math.Round(d, 2);
        }

        public static T Random<T>(this Collection<T> collection)
        {
            if (collection.Count == 0)
                throw new InvalidOperationException("Cannot return a radnom item from an empty collection");
            return collection[rng.Next(collection.Count)];
        }
    }
}
